package com.example.weather;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by asd on 2016/9/20.
 */
public class kongQiGuo extends Activity {
    private EditText et;
    private Button btn;
    private TextView tv;
    private String question = "北京天气怎么样";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kongqiguo);
        init();
        getQuestion();
    }

    private void init() {
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        tv = (TextView) findViewById(R.id.tv);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getQuestion();
                getData();
            }
        });
    }

    private void getData() {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addHeader("apikey", "6d96b140df1db1b19388daeaec241d05");
        http.send(HttpRequest.HttpMethod.GET, "http://www.tuling123.com/openapi/api?key=6b02cdfbb4444fcbbc7cc082fceb7306&info=" + question + "&userid=eb2edb736", params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject js = new JSONObject(responseInfo.result);
                    tv.setText(js.getString("text"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
        et.setText("");
    }

    public void getQuestion() {

        if (!"".equals(et.getText())) {
            question = et.getText().toString();

        }
    }
}
